package com.punai.config.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.punai.common.constant.Constants;
import com.punai.common.core.domain.CommonConstants;
import com.punai.common.exception.ServiceException;
import com.punai.common.utils.PageUtils;
import com.punai.common.utils.SecurityUtils;
import com.punai.config.constants.PnConfigConstants;
import com.punai.config.domain.ConfigInventoryChapter;
import com.punai.config.mapper.ConfigInventoryChapterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.config.mapper.ConfigInventoryLibMapper;
import com.punai.config.domain.ConfigInventoryLib;
import com.punai.config.service.IConfigInventoryLibService;

/**
 * 清单库Service业务层处理
 *
 * @author zxw_
 * @date 2023-03-22
 */
@Service
public class ConfigInventoryLibServiceImpl extends ServiceImpl<ConfigInventoryLibMapper,ConfigInventoryLib> implements IConfigInventoryLibService
{
    @Autowired
    private ConfigInventoryLibMapper configInventoryLibMapper;

    @Autowired
    private ConfigInventoryChapterMapper configInventoryChapterMapper;
    /**
     * 查询清单库
     *
     * @param id 清单库主键
     * @return 清单库
     */
    @Override
    public ConfigInventoryLib selectConfigInventoryLibById(String id)
    {
        return configInventoryLibMapper.selectConfigInventoryLibById(id,SecurityUtils.getCompanyId());
    }

    /**
     * 查询清单库列表
     *
     * @param configInventoryLib 清单库
     * @return 清单库
     */
    @Override
    public List<ConfigInventoryLib> selectConfigInventoryLibList(ConfigInventoryLib configInventoryLib)
    {
        String chapterId = configInventoryLib.getChapterId();
        Long companyId = configInventoryLib.getCompanyId();
        if (StrUtil.isEmpty(chapterId) || StrUtil.equals(chapterId, CommonConstants.DEF_PID)) {
            PageUtils.startPage();
            return configInventoryLibMapper.selectConfigInventoryLibList(configInventoryLib, null);
        }
        List<String> chapterIds = new ArrayList<>();
        chapterIds.add(chapterId);
        List<ConfigInventoryChapter> children = configInventoryChapterMapper.selectChildById(chapterId,companyId);
        if (CollUtil.isNotEmpty(children)) {
            for (ConfigInventoryChapter child : children) {
                chapterIds.add(child.getId());
            }
        }
        PageUtils.startPage();
        return configInventoryLibMapper.selectConfigInventoryLibList(configInventoryLib,chapterIds);
    }

    /**
     * 新增清单库
     *
     * @param configInventoryLib 清单库
     * @return 结果
     */
    @Override
    public int insertConfigInventoryLib(ConfigInventoryLib configInventoryLib)
    {

        String listCoding = configInventoryLib.getListCoding();
        if (isDuplication(listCoding,configInventoryLib.getCompanyId(),null)) {
            throw new ServiceException("重复的清单编码!");
        }
        String chapterId = configInventoryLib.getChapterId();
        // 获取 专业 分部
        ConfigInventoryChapter configInventoryChapter = configInventoryChapterMapper.selectById(chapterId);
        String chapterType = configInventoryChapter.getChapterType();
        if (StrUtil.equals(PnConfigConstants.CHAPTER_ZY_TYPE,chapterType)) {
            configInventoryLib.setProfessionalEngineering(chapterId);
        } else if (StrUtil.equals(PnConfigConstants.CHAPTER_FBGC_TYPE,chapterType)) {
            String pid = configInventoryChapter.getPid();
            configInventoryLib.setProfessionalEngineering(pid);
            configInventoryLib.setDivisionEngineering(chapterId);
        } else {
            String pids = configInventoryChapter.getPids();
            List<String> split = StrUtil.split(pids, Constants.SEPARATOR);
            if (split.size() > 2) {
                String zy = split.get(1);
                String fb = split.get(2);
                configInventoryLib.setProfessionalEngineering(zy);
                configInventoryLib.setDivisionEngineering(fb);
            }
        }

        return configInventoryLibMapper.insert(configInventoryLib);
    }

    // 重复
    private Boolean isDuplication(String listCoding,Long companyId,String id) {
        Long aLong = configInventoryLibMapper.selectCount(
                Wrappers.<ConfigInventoryLib>lambdaQuery()
                        .eq(ConfigInventoryLib::getCompanyId, companyId)
                        .eq(ConfigInventoryLib::getListCoding, listCoding)
                        .ne(StrUtil.isNotEmpty(id),ConfigInventoryLib::getId,id)
        );
        return aLong > 0;
    }


    /**
     * 修改清单库
     *
     * @param configInventoryLib 清单库
     * @return 结果
     */
    @Override
    public int updateConfigInventoryLib(ConfigInventoryLib configInventoryLib)
    {
        if (isDuplication(configInventoryLib.getListCoding(),configInventoryLib.getCompanyId(),configInventoryLib.getId())) {
            throw new ServiceException("重复的清单编码!");
        }
        return configInventoryLibMapper.update(configInventoryLib,
                Wrappers.<ConfigInventoryLib>lambdaUpdate()
                        .eq(ConfigInventoryLib::getCompanyId,configInventoryLib.getCompanyId())
                        .eq(ConfigInventoryLib::getId,configInventoryLib.getId())
                );
    }

    /**
     * 批量删除清单库
     *
     * @param ids 需要删除的清单库主键
     * @return 结果
     */
    @Override
    public int deleteConfigInventoryLibByIds(String[] ids)
    {
        return configInventoryLibMapper.deleteConfigInventoryLibByIds(ids, SecurityUtils.getCompanyId());
    }

}
